package com.qulei.demo.db.builder;

import android.content.ContentValues;
import android.database.Cursor;

import com.qulei.demo.db.utils.TableHelper;

public class DBBuilderImpl<T> implements DBBuilder<T> {

	private TableHelper<T> tableHelper;

	public DBBuilderImpl(Class<T> clazz) {
		tableHelper = new TableHelper<T>(clazz);
	}

	@Override
	public T build(Cursor c) {
		try {
			return (T) tableHelper.convertCursorToObject(c);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		} catch (InstantiationException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public ContentValues deconstruct(T t, int type) {
		try {
			ContentValues values = tableHelper.convertObjectToContentValues(t,
					type);
			return values;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return null;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String tableName() {
		return tableHelper.getTableName();
	}

	@Override
	public String createTableName() {
		return tableHelper.createTable();
	}

	@Override
	public String dropTableName() {
		return tableHelper.dropTable();
	}

}
